package com.nisco.dms.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nisco.dms.entity.Company;
import com.nisco.dms.exception.ServiceException;
import com.nisco.dms.service.BaseService;
import com.nisco.dms.service.CompanyService;
import com.nisco.dms.service.DeptService;
import com.nisco.dms.util.ControllerUtil;
import com.nisco.dms.util.dwz.AjaxObject;
import com.nisco.dms.util.dwz.Page;

/**
 * 
 * @author Robin
 * 
 */
@Controller
@RequestMapping(value = "company")
public class CompanyController extends AbstarctCore<Company>{
	
	
	@Autowired
	private CompanyService companyService;
//	public CompanyController(){
//		baseService = companyService;
//	}
	@Autowired
	private DeptService deptService;
	private static final String CREATE = "company/companyCreate";
	private static final String UPDATE = "company/companyUpdate";
	private static final String LIST = "company/companyList";
	private static final String SEARCH = "company/companySearch";
	private static String  moduleListNavId = "";
	@RequiresPermissions("Company:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate() {
		return CREATE;
	}

	@RequiresPermissions("Company:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody
	String create(Company company, HttpServletRequest request) {
		companyService.save(company);
		AjaxObject ajaxObject = new AjaxObject(200, "公司添加成功！",
				moduleListNavId, "", "", "closeCurrent");
		return ajaxObject.toString();
	}

	@RequiresPermissions("Company:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable String id, Map<String, Object> map) {
		Company company = companyService.get(id);

		map.put("company", company);
		return UPDATE;
	}

	@RequiresPermissions("Company:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody
	String update(Company company) {
		companyService.update(company);

		AjaxObject ajaxObject = new AjaxObject(200, "公司修改成功！",
				moduleListNavId, "", "", "closeCurrent");
		return ajaxObject.toString();
	}

	@RequiresPermissions("Company:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody
	String delete(@PathVariable String id) {
		AjaxObject ajaxObject = new AjaxObject("公司删除成功！");
		try {
			companyService.delete(id);
		} catch (ServiceException e) {
			ajaxObject.setStatusCode(AjaxObject.STATUS_CODE_FAILURE);
			ajaxObject.setMessage("公司删除失败：" + e.getMessage());
		}
		// 必须设置callBackType否则会调制主页面
		ajaxObject.setCallbackType("");
		return ajaxObject.toString();
	}

	/**
	 * 查询主要方法，其中参数company是用来做高级查询的
	 * @param page
	 * @param keywords
	 * @param map
	 * @param company
	 * @return
	 */
	@RequiresPermissions("Company:view")
	@RequestMapping(value = "/list", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String list(Page page, String keywords, Map<String, Object> map,
			Company company,HttpServletRequest request) {
		List<Company> companies = null;
//		String compId = company.getCompId();
//		String compName = company.getCompName();
//		String memo = company.getMemo();
//		if (StringUtils.isBlank(compId) && StringUtils.isBlank(compName)
//				&& StringUtils.isBlank(memo) && StringUtils.isBlank(keywords)) {
//			companies = companyService.findAll(page);
//		} else {
//			if (StringUtils.isNotBlank(keywords)) {
//				companies = companyService.find(keywords, page);
//			} else {
//				companies = companyService.find(compId, compName, memo, page);
//			}
//		}
//
//		
//		map.put("page", page);
//		map.put("companies", companies);
//		map.put("keywords", keywords);
//		if(company != null){
//		companies = companyService.searchMybatis(company,null, page);
//		}
		moduleListNavId = ControllerUtil.navId(moduleListNavId, (String)request.getParameter("moduleListNavId"));
		int totalSize = 0;
		if(keywords != null && !keywords.equals("")){
			totalSize = companyService.searchTotalMybatis(keywords);
			page.setTotalCount(totalSize);
			companies = companyService.searchMybatis(company,keywords, page);
		}
		else{
			totalSize = companyService.searchTotalMybatis(company);
			page.setTotalCount(totalSize);
			companies = companyService.searchMybatis(company,null, page);
		}
		//获取总页数
		page.setTotalPage(totalSize%page.getNumPerPage()==0?totalSize/page.getNumPerPage():(totalSize/page.getNumPerPage()+1));
		map.put("page", page);
		map.put("companies", companies);
		
		return LIST;
	}

	@RequiresPermissions("Company:view")
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public String highSearch() {
		return SEARCH;
	}

	@RequiresPermissions("Company:view")
	@RequestMapping(value = "/searchList", method = { RequestMethod.GET,
			RequestMethod.POST })
	public @ResponseBody
	String highSearch(Page page, Company company, Map<String, Object> map) {
		List<Company> companies = null;
//		String compId = company.getCompId();
//		String compName = company.getCompName();
//		String memo = company.getMemo();
//		if (StringUtils.isBlank(compId) && StringUtils.isBlank(compName)
//				&& StringUtils.isBlank(memo)) {
//			companies = companyService.findAll(page);
//		} else {
//			companies = companyService.find(compId, compName, memo, page);
//		}

		companies = companyService.searchMybatis(company,null,page);
		map.put("page", page);
		map.put("companies", companies);
//		AjaxObject ajaxObject = new AjaxObject(200,"进阶查询成功！","","","","");
//		return ajaxObject.toString();
		return LIST;
	}

	public BaseService<Company, String> test() {
		return companyService;
	}


	
}
